var app = getApp()
Page({
  data: {
    
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.type){
      that.setData({
        operaType: e.type
      })
    }
    wx.getStorage({
      key: "curEditaddress",
      success: function (res) {
        console.log('编辑地址-----');
        console.log(res);
        var region = [res.data.province, res.data.city, res.data.area];
        that.setData({
          name: res.data.name,
          mobile: res.data.mobile,
          addressdetail: res.data.address,
          code: res.data.post,
          editId: res.data.id,
          curChooseAddress: res.data.street,
          curChooseLatitude: res.data.lat,
          curChooseLongitude: res.data.lng,
          isdefault: res.data.isdefault,
          region: region
        })
      }
    })

  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({ 
      success: (res) => {
        console.log(res);
        that.setData({
          curChooseAddress: res.name ? res.name : res.address,
          curChooseLatitude: res.latitude,
          curChooseLongitude: res.longitude
        })
      },
      fail:(res)=>{
        console.log('获取位置',res);
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
              that.setData({
                isShowsetting: true
              })
            } else {
              that.setData({
                locationTips: "无法获取您的当前定位地址，请打开定位权限"
              })
            }
          }
        })
      }
    })
  },
  hideSetting: function () {
    var that = this;
    that.setData({
      isShowsetting: false
    })
  },
  openSetting: function () {
    var that = this;
    that.hideSetting();
    wx.openSetting({
      success: (res) => {
        res.authSetting = {
          "scope.userLocation": true
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  inputChange: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]: e.detail.value
    })
  },
  saveAddress: function () {
    var that = this;
    var region = that.data.region;
    var data = {};
    data.map = 'applet_address_add';
    data.name = that.data.name;
    data.mobile = that.data.mobile;
    data.address = that.data.addressdetail ? that.data.addressdetail:'';
    data.pro = region[0];
    data.city = region[1];
    data.area = region[2];
    // data.pro = that.data.curChooseAddress ? that.data.curChooseAddress:'';
    data.pcda = that.data.curChooseAddress ? that.data.curChooseAddress : '';
    data.lat = that.data.curChooseLatitude;
    data.lng = that.data.curChooseLongitude;
    data.code = that.data.code;
    data.id = that.data.editId;
    if (!data.name) {
      app.errorTip(that, '请输入姓名', 2000);
      return;
    }
    if (!data.mobile) {
      app.errorTip(that, '请输入正确的手机号', 2000);
      return;
    }
	if (!data.pro || !data.city || !data.area) {
	  app.errorTip(that, '请选择省市区', 2000);
	  return;
	}
    if (!data.pcda) {
      app.errorTip(that, '请选择地址信息', 2000);
      return;
    }
    
    if (!data.address) {
      app.errorTip(that, '请输入详细地址', 2000);
      return;
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          console.log(that.data.operaType);
          if (that.data.operaType == 'waitorder') {
            var curAddressData = res.data.data.address;
            // 存储当前编辑地址信息
            console.log(curAddressData);
            wx.getStorage({
              key: 'submitOrder',
              success: function (res) {
                console.log(res.data)
                var orderInfo = res.data;
                orderInfo.address = curAddressData;
                wx.setStorage({
                  key: "submitOrder",
                  data: orderInfo,
                  success: function () {
                    wx.navigateBack({
                      delta: 1
                    })
                    console.log('更改后的地址更新-----');
                    console.log(orderInfo);
                  }
                })
              }
            })
          } else {
            wx.navigateBack({
              delta: 1
            })
          }
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  deleteAddress: function (e) {
    var delId = e.target.dataset.editid;
    wx.showModal({
      title: '',
      content: '确认删除该收货地址吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showLoading({
            title: '正在删除',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_address_delete',
              suid: app.globalData.suid,
              id: delId
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                wx.showModal({
                  title: '提示',
                  content: res.data.data.msg,
                  showCancel: false,
                  success: function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                });
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.em,
                  showCancel: false
                });
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onUnload: function () {
    wx.removeStorage({
      key: 'curEditaddress',
      success: function (res) {
        // console.log("清楚缓存成功")
      }
    })
  }
})